import requset from "@/utils/requset";
import request from "@/utils/requset";

// 文章详情
const articleDetail = (article_id) => {
  return request({
    url: `/v1_0/articles/${article_id}`,
    method: "GET",
  });
};

// 关注
const followingsUser = (id) => {
  return request({
    url: "/v1_0/user/followings",
    method: "POST",
    data: {
      target: id,
    },
  });
};

// 取消关注
const cancelFollowingsUser = (id) => {
  return request({
    url: `/v1_0/user/followings/${id}`,
    method: "DELETE",
  });
};

//点赞
const likeArticle = ({ id, flag }) => {
  if (flag == true) {
    // 点赞
    return request({
      url: "/v1_0/article/likings",
      method: "POST",
      data: {
        target: id,
      },
    });
  } else {
    // 取消点赞
    return request({
      url: `/v1_0/article/likings/${id}`,
      method: "DELETE",
    });
  }
};

// 获取评论或评论回复
const getComment = ({ id, offset = null, limit = 10 }) => {
  console.log(id);
  return request({
    url: "/v1_0/comments",
    method: "GET",
    params: { type: "a", source: id, offset, limit },
  });
};

// 评论喜欢
const commentLikingAPI = ({ comId }) => {
  return request({
    url: `/v1_0/comment/likings`,
    method: "POST",
    data: {
      target: comId,
    },
  });
};
// 评论不喜欢
const commentDisLikingAPI = ({ comId }) => {
  return requset({
    url: `/v1_0/comment/likings/${comId}`,
    method: "DELETE",
  });
};

//发布评论
const articleCmment = ({ id, content, art_id = null }) => {
  let obj = { target: id, content: content };
  if (art_id != null) {
    obj.art_id = art_id;
  }
  return requset({
    url: "/v1_0/comments",
    method: "post",
    data: obj,
  });
};

export {
  articleDetail,
  followingsUser,
  cancelFollowingsUser,
  likeArticle,
  getComment,
  commentDisLikingAPI,
  commentLikingAPI,
  articleCmment,
};
